<template>
  <view class="my-page">
    <!-- 登录状态判断 -->
    <view v-if="isLoggedIn">
      <!-- 用户信息区 -->
      <view class="user-info-card">
        <image :src="userInfo.avatar" class="avatar" />
        <view class="user-details">
          <text class="name">{{ userInfo.nickname }}</text>
          <text class="store">{{ userInfo.bio }}</text>
          <view class="role-tag">
            <image src="/static/location.png" class="location-icon" />
            <text>vip</text>
          </view>
        </view>
        <button class="check-in-btn">签到</button>
      </view>
	</view>
	<view v-else class="not-logged-in">
	  <view class="login-prompt">
	    <text class="prompt-text">您还未登录</text>
	    <button class="login-btn" @click="goToLogin">立即登录</button>
	  </view>
	</view>
      <!-- 功能列表 -->
      <view class="features">
        <view class="feature-item" @click="goToFavorites">
          <image src="/static/icon_favorite.png" class="feature-icon" />
          <text class="feature-text">我的收藏</text>
          <image src="/static/arrow-right.png" class="arrow-icon" />
        </view>
        <view class="feature-item" @click="goToConsults">
          <image src="/static/icon_consult.png" class="feature-icon" />
          <text class="feature-text">咨询记录</text>
          <image src="/static/arrow-right.png" class="arrow-icon" />
        </view>
        <view class="feature-item" @click="goToExamMistakes"> <!-- 新增 -->
          <image src="/static/icon_exam_wrong.png" class="feature-icon" />
          <text class="feature-text">法考错题</text>
          <image src="/static/arrow-right.png" class="arrow-icon" />
        </view>
        <view class="feature-item" @click="goToCases">
          <image src="/static/icon_case.png" class="feature-icon" />
          <text class="feature-text">案件追踪</text>
          <image src="/static/arrow-right.png" class="arrow-icon" />
        </view>
        <view class="feature-item" @click="goToAbout">
          <image src="/static/icon_about.png" class="feature-icon" />
          <text class="feature-text">关于我们</text>
          <image src="/static/arrow-right.png" class="arrow-icon" />
        </view>
      </view>
  </view>
</template>

<script>
import { mapState, mapGetters, mapActions } from 'vuex';

export default {
  data() {
    return {
      isLoggedIn: false,
      userInfo: {
        avatar: '/static/avatar.png',
        nickname: '用户名',
        bio: '简介'
      }
    };
  },
  onLoad() {
    this.checkLoginStatus();
  },
  onShow() { // 每次进入页面都检查一次，防止在其他页面登录后状态未更新
    this.checkLoginStatus();
  },
  computed: {
      ...mapState(['token', 'userInfo', 'isLoggedIn']),
      ...mapGetters(['userAvatar', 'userNickname'])
	},
  methods: {
    checkLoginStatus() {
      const token = uni.getStorageSync('token');
      const userInfo = uni.getStorageSync('userInfo');
      const isLoggedIn = uni.getStorageSync('isLoggedIn');

      if (token && userInfo && isLoggedIn) {
        this.isLoggedIn = true;
        this.userInfo = userInfo;
      } else {
        this.isLoggedIn = false;
        this.userInfo = {
          avatar: '/static/avatar.png',
          nickname: '用户名',
          bio: '简介'
        };
      }
    },
    goToFavorites() {
      if (!this.isLoggedIn) {
        this.goToLogin();
        return;
      }
      uni.navigateTo({ url: '/pages/favorites/favorites' });
    },
    goToConsults() {
      if (!this.isLoggedIn) {
        this.goToLogin();
        return;
      }
      uni.navigateTo({ url: '/pages/consult-history/consult-history' });
    },
    goToExamResults() {
      if (!this.isLoggedIn) {
        this.goToLogin();
        return;
      }
      uni.navigateTo({ url: '/pages/exam-results/exam-results' });
    },
    goToExamMistakes() { // 新增
      if (!this.isLoggedIn) {
        this.goToLogin();
        return;
      }
      uni.navigateTo({ url: '/pages/exam-mistakes/exam-mistakes' });
    },
    goToCases() {
      if (!this.isLoggedIn) {
        this.goToLogin();
        return;
      }
      uni.navigateTo({ url: '/pages/case-track/case-track' });
    },
    goToAbout() {
      uni.navigateTo({ url: '/pages/about/about' });
    },
    goToLogin() {
      uni.navigateTo({ url: '/pages/login/login' });
    }
  }
}
</script>

<style scoped>
.my-page {
  background-color: #f8f8f8;
  min-height: 100vh;
  padding-bottom: 120rpx;
}

/* 用户信息卡片 - 优化后 */
.user-info-card {
  display: flex;
  align-items: center;
  padding: 30rpx;
  background-color: #fff;
  border-bottom: 1px solid #eee;
  box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.05);
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  margin-right: 20rpx;
}

.user-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  line-height: 1.5;
}

.name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.store {
  font-size: 26rpx;
  color: #666;
}

.role-tag {
	width: 80rpx;
  display: inline-flex;
  align-items: center;
  background-color: #007aff;
  color: white;
  padding: 4rpx 12rpx;
  border-radius: 16rpx;
  font-size: 24rpx;
  margin-top: 10rpx;
}

.location-icon {
  width: 20rpx;
  height: 20rpx;
  margin-right: 6rpx;
}

.check-in-btn {
  padding: 10rpx 20rpx;
  border: 1px solid #007aff;
  border-radius: 30rpx;
  color: #007aff;
  font-size: 26rpx;
  background-color: transparent;
  align-self: center; 
}

/* 功能列表容器 */
.features {
  padding: 20rpx;
  background-color: #fff;
  margin-top: 20rpx;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.05);
}

.feature-item {
  display: flex;
  align-items: center;
  padding: 25rpx 0;
  border-bottom: 1px solid #eee;
  position: relative;
  transition: background-color 0.2s ease;
}

.feature-item:last-child {
  border-bottom: none;
}

.feature-item:active {
  background-color: #f5f5f5;
}

.feature-icon {
  width: 40rpx;
  height: 40rpx;
  margin-right: 20rpx;
}

.feature-text {
  flex: 1;
  font-size: 30rpx;
  color: #333;
}

.arrow-icon {
  width: 32rpx;
  height: 32rpx;
  opacity: 0.5;
}
/* 未登录状态样式 */
.not-logged-in {
  min-height: 10vh;
  display: flex;
  flex-direction: column;
}

.login-prompt {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #fff;
  padding: 100rpx 0;
}

.prompt-text {
  font-size: 32rpx;
  color: #333;
  margin-bottom: 40rpx;
}

.login-btn {
  padding: 10rpx 40rpx;
  background-color: #007aff;
  color: white;
  border: none;
  border-radius: 10rpx;
  font-size: 30rpx;
}
</style>